package com.citcc.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.citcc.api.config.Authorization;
import com.citcc.business.bean.*;
import com.citcc.business.bean.vo.CommunityH5VO;
import com.citcc.business.bean.vo.ModelVo;
import com.citcc.business.bean.vo.ServiceVo;
import com.citcc.business.bean.vo.VersionVO;
import com.citcc.business.service.H5Service;
import com.citcc.common.utils.StringUtils;
import com.citcc.framework.base.BaseController;
import com.citcc.framework.sms.JsonResult;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.*;

/**
 * creat_user: 秦波
 * describe: TODO
 **/
@Api(tags = "服务相关接口（包含最新天气，油价查询）")
@RestController
public class ServiceH5Controller extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ServiceH5Controller.class);
    @Autowired
    private H5Service h5Service;

    /**
     * 查询服务模块
     *
     * @param
     * @return
     */
    @ApiOperation("订单列表 type 0:全部 1，充电中，2，已完成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "0：服务，1：钱包", paramType = "query")
    })
    @RequestMapping(value = "/v3/services", method = RequestMethod.POST)

    public Object me(HttpServletRequest request, @Authorization CdzAppUser cdzAppUser, @RequestParam String type) {
        List<TCdzSerTypeInfo> list = h5Service.findAll(type, "", "");
        List<ServiceVo> serviceVos = new ArrayList<>();
        if (list != null && list.size() > 0) {

            for (TCdzSerTypeInfo appSerTypeEntity : list) {
                ServiceVo serviceVo = new ServiceVo();
                String tittle = appSerTypeEntity.getTitle();
                String url = appSerTypeEntity.getLogoPicUrl();
                serviceVo.setLogo(url);
                serviceVo.setName(tittle);

                String id = appSerTypeEntity.getId();
                //根据id查询
                List<TCdzSerTypeModule> models = h5Service.findModel(id, "", "");
                List<ModelVo> vos = new ArrayList<>();
                for (TCdzSerTypeModule model : models) {
                    ModelVo modelVo = new ModelVo();
                    modelVo.setLinkUrl(model.getLinkUrl());
                    modelVo.setId(model.getId());
                    modelVo.setPhoto(model.getLogoPicUrl());
                    modelVo.setSort(model.getSort());
                    modelVo.setTitle(model.getTitle());
                    modelVo.setType(model.getType());
                    modelVo.setHiddenNav("0".equals(model.getIsHiddenNav()) ? false : true);
                    modelVo.setUseUIWeb("0".equals(model.getIosWebviewFlag()) ? false : true);
                    modelVo.setUseWebTitle("0".equals(model.getIsH5title()) ? false : true);
                    modelVo.setUseLocation("0".equals(model.getIsGps()) ? false : true);
                    String isH5 = model.getIsH5url();//0:不是
                    modelVo.setIsH5Url(isH5);//新增是否为h5url
                    modelVo.setNeedLogin(model.getIsLogin());
                    vos.add(modelVo);
                }
                if (vos != null && vos.size() > 0) {
                    serviceVo.setModels(vos);
                    serviceVos.add(serviceVo);
                }

            }
        }

        return success(200, serviceVos);
    }


    /**
     * 轮播图  使用
     *
     * @param
     * @return
     */
    @ApiOperation("轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "0：服务，1：钱包", paramType = "query")
    })
    @RequestMapping(value = "/v3/community/h5/resources", method = RequestMethod.GET)
    public Object geth5Resourc(HttpServletRequest request, String type) {

        List<CommunityH5VO> communityH5VOs = new ArrayList<>();
        try {
            List<ActivityResourcesEntity> list = h5Service.findH5resourcess(type);
            if (list != null && list.size() > 0) {
                //功能区域   type:4  19.特权 appSource :1  车主通
                for (ActivityResourcesEntity consultCarouselPicEntity : list) {
                    CommunityH5VO communityH5VO = new CommunityH5VO();
                    communityH5VO.setLinkUrl(consultCarouselPicEntity.getLinkUrl());

                    communityH5VO.setIsH5url(consultCarouselPicEntity.getIsH5url());

                    communityH5VO.setId(consultCarouselPicEntity.getId());

                    communityH5VO.setPicUrl(consultCarouselPicEntity.getPicUrl());
                    communityH5VO.setTitle(consultCarouselPicEntity.getTitle());
                    // communityH5VO.setPublishDate(DateUtil.formatDateTString(consultCarouselPicEntity.getPublishDate()));
                    communityH5VO.setSort(Integer.parseInt(consultCarouselPicEntity.getSort()));
                    communityH5VO.setType(consultCarouselPicEntity.getType());

                    communityH5VO.setHiddenNav("0".equals(consultCarouselPicEntity.getIsHiddenNav()) ? false : true);
                    communityH5VO.setUseUIWeb("0".equals(consultCarouselPicEntity.getIosWebviewFlag()) ? false : true);
                    communityH5VO.setUseWebTitle("0".equals(consultCarouselPicEntity.getIsH5title()) ? false : true);
                    communityH5VO.setUseLocation("0".equals(consultCarouselPicEntity.getIsGps()) ? false : true);
                    if (consultCarouselPicEntity.getPublishDate() != null) {
                        communityH5VO.setPublishDate(Long.toString(consultCarouselPicEntity.getPublishDate().getTime()));
                    }
                    communityH5VO.setNeedLogin(consultCarouselPicEntity.getIsLogin());
                    communityH5VOs.add(communityH5VO);
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            logger.error(e.getMessage());
            logger.info("轮播图异常");
        }
            return success(200,communityH5VOs);
    }

    /**
     * 版本更新
     * @return
     */
    @ApiOperation("版本更新")
   @RequestMapping(value = "/app/version/info", method = RequestMethod.POST)
    public void getVersion(HttpServletResponse response) {
        VersionVO version =new VersionVO();
        response.setContentType("application/Json");
        VersionInfoEntity version1 = h5Service.getVersion();
        JSONObject subJson = new JSONObject();
        if (version1!=null){
            subJson.put("code",200);
            if (StringUtils.isNotEmpty(version1.getAndroidDescribe())){
                version.setAndroidDescribe(version1.getAndroidDescribe().replace("\\n","\n"));
            }else{
                version.setAndroidDescribe("");
            }
            if (StringUtils.isNotEmpty(version1.getIosDescribe())){
                version.setIosDescribe(version1.getIosDescribe().replace("\\n","\n"));
            }else{
                version.setIosDescribe("");
            }
            version.setAndroidUpdate(version1.getAndroidUpdate());
            version.setAndroidDownload(version1.getAndroidDownload());
            version.setAndroidVersion(version1.getAndroidVersion());
            version.setIosUpdate(version1.getIosUpdate());
            version.setIosDownload(version1.getIosDownload());
            version.setIosVersion(version1.getIosVersion());
            subJson.put("data",version);
        }else {
            subJson.put("code","-1");
        }
        try{
            OutputStream ow = response.getOutputStream();
            ow.write(subJson.toString().getBytes());
            ow.flush();
            ow.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
